package com.toolbox.controller;

import com.toolbox.service.ToolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/tools/ppt")
public class PPTController {

    @Autowired
    private ToolService toolService;

    /**
     * PPT生成接口
     * @param request 请求参数，包含title、templateType和slides
     * @return 生成的PPT文件
     */
    @PostMapping("/generate")
    public ResponseEntity<byte[]> generatePPT(@RequestBody Map<String, Object> request) {
        
        try {
            // 获取请求参数
            String title = (String) request.get("title");
            String templateType = (String) request.get("templateType");
            List<Map<String, String>> slides = (List<Map<String, String>>) request.get("slides");
            
            // 参数验证
            if (title == null || title.trim().isEmpty()) {
                return ResponseEntity.badRequest().body("请输入PPT标题".getBytes());
            }
            
            if (templateType == null || templateType.trim().isEmpty()) {
                return ResponseEntity.badRequest().body("请选择模板类型".getBytes());
            }
            
            if (slides == null || slides.isEmpty()) {
                return ResponseEntity.badRequest().body("请至少添加一张幻灯片".getBytes());
            }
            
            // 验证模板类型是否支持
            if (!templateType.equals("business") && !templateType.equals("education") && !templateType.equals("report")) {
                return ResponseEntity.badRequest().body("不支持的模板类型，仅支持business、education、report".getBytes());
            }
            
            // 准备请求参数
            Map<String, Object> requestParams = new HashMap<>();
            requestParams.put("title", title);
            requestParams.put("templateType", templateType);
            requestParams.put("slides", slides);
            
            // 调用服务层生成PPT
            byte[] pptData = toolService.generatePPT(requestParams);
            
            // 生成响应头
            HttpHeaders headers = new HttpHeaders();
            String fileName = title.replaceAll("[\\/:*?\"<>|]", "_") + ".pptx";
            
            // 设置响应头
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            headers.setContentDispositionFormData("attachment", fileName);
            headers.setContentLength(pptData.length);
            
            return new ResponseEntity<>(pptData, headers, HttpStatus.OK);
            
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().body(e.getMessage().getBytes());
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("PPT生成失败".getBytes());
        }
    }
}